<?php
// $Id$

/**
 * Controller_Admin_Web 控制器
 */
class Controller_Admin_videopintuan extends Controller_Abstract {
	
	/**
	 * 视频拼团
	 */
	function actionIndex()
	{

		$this->union_table = videopintuan::meta();
		$this->_view ['video_id'] = $this->_context->video_id;
		$this->lists();
	}
	 
	/**
	 * 视频拼团设置
	 */
	 
	function actionAdd() {
	    //设置要操作的表单，并设置自动插入方式
	    $this->union_table = videopintuan::meta ();
	    $handle = new helper_autoinsert ( $this->union_table );

	    $form = new Form_videopintuan_add ( url ( $this->_context->controller_name . '/' . $this->_context->action_name ) );
 
	    //这里面会自动将get方式获取的参数传递到表单里面去的
	    $result = $handle->add ( $form, 'id', $_GET );
	    if ($this->_context->isGET ()) {
	        //传递$form值
	        $this->_view ['form'] = $result;
	        $this->_viewname = '../form/add';
	        $form->element('acct_id')->value = $_SESSION['acct']['id'];
	        $form->element('video_id')->value = $this->_context->video_id;
	        if($this->_context->id){
	        	$info = videopintuan::find(array("id"=>$this->_context->id))->getOne();
	        	$form->element('end_time')->value = date("Y-m-d",$info->end_time);
	        }

	    } else {

	        //记录日志
	        if(!$_POST['id'])	{
	        	//修改video 是否拼团字段
	        	$video = video::find(array("id"=>intval($this->_context->video_id)) )->getOne();
	        	$video->is_pintuan = 1;
	        	$video->save();

	            Syslog::logs ( '添加视频拼团,id :' . $handle->form_database_obj->id );
	        }else{
	            Syslog::logs ( '编辑视频拼团,id :' . $handle->form_database_obj->id );
	        }
	         
	        if($handle->status=='success')
	            $this->_context->forwardUrl = url("videopintuan")."/index/video_id/".$this->_context->video_id;
	        return call_user_func_array(array($this,$handle->status),array($result));
	    }
	     
	}
	 
	/**
	 * 删除视频拼团
	 */
	function actionDel() {
		Syslog::logs("删除视频拼团，ID：".print_r($this->_context->ids,true));
		$this->union_table = videopintuan::meta();
		
		$pintuan = videopintuan::find(array("id" => intval($this->_context->ids[0])))->getOne();
		$video_id = $pintuan->video_id;
		//删除操作
		QDB::getConn()->startTrans();
		try {
		    $this->union_table->deleteWhere(array('id'=>$this->_context->ids));
		    QDB::getConn()->completeTrans(true);	
		} catch (Exception $e) {
		    QDB::getConn()->completeTrans(FALSE);
	    	return $this->error($e->getmessage());	
		}
		//判断拼团是否全部删除
		$pintuan_lis = videopintuan::find(array("video_id" => $video_id))->asArray()->getAll();

    	if(empty($pintuan_lis)){
    		$video = video::find("id = $video_id")->getOne();
    		$video->is_pintuan = 0;
	        $video->save();
	        Syslog::logs("修改视频拼团，ID：".print_r($video_id,true));
    	}
		return $this->success('删除成功');

	    
	    
	}


}


